package com.hoow.shakoow.components.dialog

import android.os.Bundle
import android.view.*
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.hoow.shakoow.R
import com.hoow.shakoow.databinding.DialogUpdateNickNameAndGenderBinding

class UpdateNickNameAndGenderDialog : DialogFragment() {

    companion object {
        /** 展示dialog */
        fun showDialog(
            fragmentManager: FragmentManager,
            name: String,
            gender: Int,
            listener: (String, String) -> Unit = { _, _ -> },
        ) {
            val dialog = UpdateNickNameAndGenderDialog()
            dialog.mOnActionListener = listener
            dialog.mName = name
            dialog.mGender = gender.toString()
            dialog.showNow(fragmentManager, UpdateNickNameAndGenderDialog::class.java.simpleName)
        }
    }

    private lateinit var mViewBinding: DialogUpdateNickNameAndGenderBinding
    private var mOnActionListener: (String, String) -> Unit = { name, gender -> }
    private var mGender: String = "1"
    private var mName: String = ""

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)

        mViewBinding = DialogUpdateNickNameAndGenderBinding.inflate(layoutInflater)
        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initSubViews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        mViewBinding.editNickName.setText(mName)
        // updateGenderStateUI()
    }

    private fun initSubViews() {
        // 点击男
        mViewBinding.llMaleBg.setOnClickListener {
            mGender = "1"
            updateGenderStateUI()
        }

        // 点击妇
        mViewBinding.llFemaleBg.setOnClickListener {
            mGender = "0"
            updateGenderStateUI()
        }
        mViewBinding.textConfirm.setOnClickListener {
            val name = mViewBinding.editNickName.text.toString().trim()
            mOnActionListener.invoke(name, mGender)
            dismiss()
        }
    }

    private fun updateGenderStateUI() {
        if (mGender.equals("1", true)) {
            mGender = "1"

            mViewBinding.llMaleBg.setBackgroundResource(R.drawable.bg_gender_selected)
            mViewBinding.llFemaleBg.setBackgroundResource(R.drawable.bg_gender_unselect)
            mViewBinding.imageGenderMale.setImageResource(R.drawable.icon_male)
            mViewBinding.imageGenderFemale.setImageResource(R.drawable.icon_female_no)

            mViewBinding.textMale.setTextColor(
                ContextCompat.getColor(
                    requireContext(),
                    R.color.main_black
                )
            )
            mViewBinding.textFemale.setTextColor(
                ContextCompat.getColor(
                    requireContext(),
                    R.color.form_border
                )
            )
        } else {
            mGender = "0"

            mViewBinding.llFemaleBg.setBackgroundResource(R.drawable.bg_gender_selected)
            mViewBinding.llMaleBg.setBackgroundResource(R.drawable.bg_gender_unselect)
            mViewBinding.imageGenderMale.setImageResource(R.drawable.icon_male_no)
            mViewBinding.imageGenderFemale.setImageResource(R.drawable.icon_female)

            mViewBinding.textMale.setTextColor(
                ContextCompat.getColor(
                    requireContext(),
                    R.color.form_border
                )
            )
            mViewBinding.textFemale.setTextColor(
                ContextCompat.getColor(
                    requireContext(),
                    R.color.main_black
                )
            )
        }
    }
}